﻿Imports System.Web

Public Class NHibernateHttpModule
    Implements IHttpModule

    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init

        AddHandler context.EndRequest, AddressOf Application_EndRequest
        AddHandler context.BeginRequest, AddressOf Application_BeginRequest
    End Sub

    Public Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

        NHibernate.Context.CurrentSessionContext.Bind(SessionFactory.GetNewSession)

    End Sub

    Public Sub Application_EndRequest(ByVal sender As Object, ByVal e As EventArgs)

        Dim CurrentSession As NHibernate.ISession
        CurrentSession = NHibernate.Context.CurrentSessionContext.Unbind(SessionFactory.GetSessionFactory)

        CurrentSession.Close()
        CurrentSession.Dispose()

    End Sub

End Class